// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <algorithm>
#include <vector>

std::vector<std::vector<int>> evil;

int n;

int attck(std::vector<int> const& seq)
{
	int ret = 0, now = 0;
	for (auto i : seq) {
		for (auto j : evil[i]) {
			if (j > now) {
				ret += j - now;
				now += j - now;
			}
			now++;
		}
	}
	return ret;
}

int calc()
{
	std::vector<int> seq;
	for (int i = 0; i < n; i++) seq.push_back(i);
	int ret = 1 << 30;
	do {
		ret = std::min(ret, attck(seq));
	} while (std::next_permutation(seq.begin(), seq.end()));
	return ret;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	evil.resize(n);
	for (int i = 0; i < n; i++) {
		int m;
		std::cin >> m;
		for (int j = 0, x; j < m; j++) {
			std::cin >> x;
			evil[i].push_back(x);
		}
	}

	int q; std::cin >> q;
	for (int i = 0; i < q; i++) {
		int x, a, y, b;
		std::cin >> x >> a >> y >> b;
		x--; y--;
		std::vector<int> ta;
		std::vector<int> tb;
		for (int j = 0; j < a; j++) tb.push_back(evil[x][j]);
		for (int j = 0; j < b; j++) ta.push_back(evil[y][j]);
		for (int j = a; j < (int)evil[x].size(); j++) ta.push_back(evil[x][j]);
		for (int j = b; j < (int)evil[y].size(); j++) tb.push_back(evil[y][j]);
		evil[x] = ta;
		evil[y] = tb;
		std::cout << calc() << '\n';
	}
}

